<template>
  <div>
    <!--医护人员信息-->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getTableData()">
      <el-form-item label="姓名">
        <el-input v-model="dataForm.name" placeholder="姓名" clearable size="mini"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          @click="getTableData()"
          style="background-color: #4CAF50; color: white;margin-left: 10px; padding: 9px 15px 9px 15px"
        >查询</el-button>
        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
          style="margin-left: 10px; padding: 9px 15px 9px 15px"
        >新增</el-button>
        <el-button
          type="danger"
          @click="onDelete()"
          :disabled="dataListSelections.length <= 0"
          style="margin-left: 10px; padding: 9px 15px 9px 15px"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="账号"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="姓名"></el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="手机"></el-table-column>
      <el-table-column prop="role" header-align="center" align="center" label="角色"></el-table-column>
      <el-table-column prop="graduation_inst" header-align="center" align="center" label="毕业院校"></el-table-column>
      <el-table-column prop="education_qual" header-align="center" align="center" label="学历"></el-table-column>
      <el-table-column prop="professional_title" header-align="center" align="center" label="职称"></el-table-column>
      <el-table-column prop="professional_cert" label="职业资格证书" align="center" min-width="130px">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.professional_cert"
            :preview-src-list="[scope.row.professional_cert]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="addOrUpdateHandle(scope.row.username)" size="mini">编辑</el-button>
          <el-button type="danger" @click="onDelete(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getTableData"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update.vue";
import { getDataList, delDataList } from "@/api/user";
export default {
  name: "UserPage",
  components: { AddOrUpdate },
  data() {
    return {
      dataForm: {},
      tableData: [],
      dataListLoading: false,
      dataListSelections: [],
      pageIndex: 1,
      pageSize: 50,
      totalPage: 0,
      role: "",
      addOrUpdateVisible: false,
      roleOptions: []
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    // 获取表格数据
    getTableData() {
      getDataList({
        ...this.dataForm,
        limit: this.pageSize,
        page: this.pageIndex
      })
        .then(res => {
          if (res && res.code === 0) {
            // 如果角色输入框有值，则进行过滤
            if (this.dataForm.name) {
              this.tableData = res.data.list.filter(
                item => item.name.indexOf(this.dataForm.name) >=0
              );
            } else {
              this.tableData = res.data.list;
            }
            this.tableData = this.tableData.filter(i => i.role !== "宠物主人");
            this.totalPage = this.tableData.length; // 更新总页数
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 编辑
    addOrUpdateHandle(username) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(username);
      });
    },
    // 删除
    onDelete(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delDataList(ids)
          .then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getTableData();
                }
              });
            }
          })
          .catch(err => {
            console.log(err);
          });
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getTableData();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getTableData();
    }
  }
};
</script>

<style></style>